/**
 * Created with JetBrains WebStorm.
 * User: Eric
 * Date: 02/02/13
 * Time: 17:17
 * To change this template use File | Settings | File Templates.
 */

/*
 * This file is a test of un Dao implementation
 * We need the mysql module
 */


// We create a connection
var connector = require('./connector');
var connection = connector.connection();
// We need our model
var User = require("./User");

// a example of method to retrieve an user or a list of user from the db
var findOneByEmail = function (email, callback){ // we need a callback because the query is asynchronous

    var user = new User(); // Our model that we can instanciate
    var options = {sql: 'SELECT user.*, personage.* from user  left join personage  on (user.personage = personage.id) where user.email = ? ',
        values: email, nestTables: true};
    connection.query(options, function(err, rows, fields) { // we do the query
        if (err) throw err; // if there is an error we throw it
        if (rows.length === 1){ // there is one result

            if (rows[0]['user'].personage != null && rows[0]['personage'] != undefined){
                rows[0]['user'].personage = rows[0]['personage'];
            }

            user.init(rows[0]['user']); // we init the model with the row
        }else{
            process.stdout.write("user not found : " + email );
            user = null; // We didin't find the user
        }
       callback(user); // Callback basicaly it's the end of the controleur
    });

};

var findOneByToken = function(token, callback){
    var user = new User(); // Our model that we can instanciate
    connection.query('SELECT * from user where confirmation_token = ?', [token], function(err, rows, fields) { // we do the query
        if (err) throw err; // if there is an error we throw it
        if (rows.length === 1){ // there is one result
            user.init(rows[0]); // we init the model with the row
        }else{
            user = null; // We didin't find the user
        }
        callback(user); // Callback basicaly it's the end of the controleur
    });
}

// a example of method to retrieve an user or a list of user from the db
var findOneByPseudo = function (pseudo, callback){ // we need a callback because the query is asynchronous

    var user2 = new User(); // Our model that we can instanciate
    connection.query('SELECT * from user where pseudo = ?', [pseudo], function(err, rows, fields) { // we do the query
        if (err) throw err; // if there is an error we throw it
        if (rows.length === 1){ // there is one result
            //process.stdout.write("user found : " + pseudo );
            user2.init(rows[0]); // we init the model with the row
        }else{
            //process.stdout.write("user not found : " + pseudo );
            user2 = null; // We didin't find the user
        }
        callback(user2); // Callback basicaly it's the end of the controleur
    });

};

var saveUser = function(user, callback){

}



var persistUser = function(user, callback){

    console.log("dans persistUser : " + typeof user.getPersonageForPersist);

    var post  = {lastname: user.lastname, firstname:
        user.firstname, email: user.email, pseudo: user.pseudo, password: user.password, confirmation_token: user.confirmation_token, confirmation_date_limit: user.confirmation_date_limit,
        actif : user.actif, personage: user.getPersonageForPersist(), type: "User"};

    if (user.id == null){
        connection.query('INSERT INTO user SET ?', post, function(err, rows, fields){

            if (err) throw err; // if there is an error we throw it
            callback(user); // Callback basicaly it's the end of the controleur
        });
    }else{

        connection.query('update user SET ? where id = ?',[post,user.id], function(err, rows, fields){



            if (err) throw err; // if there is an error we throw it
            callback(user); // Callback basicaly it's the end of the controleur
        });
    }


};

exports.findOneByEmail = findOneByEmail;
exports.findOneByPseudo = findOneByPseudo;
exports.persistUser = persistUser;
exports.findOneByToken = findOneByToken;
exports.saveUser = saveUser;

